﻿using System.Data;
using System.Data.SqlClient;
using Pantry.Commons;

namespace Pantry.DataAccess
{
    public class ServicePremiseDao
    {
        private readonly SqlConnection _sqlConnection;
        public ServicePremiseDao()
        {
            _sqlConnection = SqlConnectionTool.SqlGetConnection();
        }
        #region Method
        /// <summary>
        /// Selects the service premise by service id.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <returns></returns>
        public DataTable SelectServicePremiseByServiceId(int serviceId)
        {
            try
            {
                var sqlParam = new SqlParameter[1];
                sqlParam[0] = new SqlParameter("@ServiceId", SqlDbType.Int) { Value = serviceId };
                var ds = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, DBSchemas.SqlSelectServicePremiseByServiceId, sqlParam);
                return (ds.Tables[0]);
            }
            catch (SqlException sqlException)
            {
                ExceptionHandler.WriteErrorLog(sqlException);
                throw;
            }
        }
        /// <summary>
        /// Selects the service premise not belong service.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectServicePremiseNotBelongService()
        {
            try
            {
                var ds = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, DBSchemas.SqlSelectServicePremiseNotBelongService);
                return (ds.Tables[0]);
            }
            catch (SqlException sqlException)
            {
                ExceptionHandler.WriteErrorLog(sqlException);
                throw;
            }
        }
        /// <summary>
        /// Updates the premise add to service.
        /// </summary>
        /// <param name="projectCode">The project code.</param>
        /// <param name="serviceId">The service id.</param>
        /// <param name="premiseId">The premise id.</param>
        /// <returns></returns>
        public int UpdatePremiseAddToService(string projectCode,string serviceId,int premiseId)
        {
            try
            {
                var sqlParam = new SqlParameter[3];
                sqlParam[0] = new SqlParameter("@ProjectCode",SqlDbType.NVarChar){Value = projectCode};
                sqlParam[1] = new SqlParameter("@ServiceId",SqlDbType.VarChar){Value = serviceId};
                sqlParam[2] = new SqlParameter("@PremiseId",SqlDbType.Int){Value = premiseId};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, DBSchemas.SqlUpdatePremiseAddToService,
                                                 sqlParam);
            }
            catch (SqlException sqlException)
            {
                ExceptionHandler.WriteErrorLog(sqlException);
                throw;
            }
        }
        #endregion
    }
}
